<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";

//----------------------------\\
// LIST OF SPLIT TEST GROUPS 
//----------------------------\\
if (!$action)
{
?>


<p class="small" align="left">
Split Test > Edit
</p>


<p class="heading">
Manage Split Test Groups
</p>

<u>List of Current Split Tests</u>

<p>You can modify or delete any given split test group, just click the appropriate link.</p>
<?
	//DELETE CAMPAIGNS if from delete
	if ($subaction == "delete")
	{
		$st_label = mysql_fetch_row(mysql_query("select name from adtrackz_split_test_groups where ID='$st_id'"));
		if (!$confirm)
		{
			alert("Are you sure you want to delete the split test <b>$st_label[0]</b>?");			
			?>
			<br><br>
			<table align=center>
			<tr>
				<td>
					<form action="st_edit.php" method=post>
					<input type=submit name=submit_bottom value=" Yes ">
					<input type=hidden name=subaction value=delete>
					<input type=hidden name=confirm value=yes>
					<input type=hidden name=st_id value=<?=$st_id?>>
					</form>
				</td>
				<td>
					<form action="st_edit.php" method=post>
					<input type=hidden name=cID value=<?=$cID?>>
					<input type=submit name=cancel value=" No ">
					</form>
				</td>
			</tr></table>
			<?	
			require "footer.php";
			exit;
		}
		else
		{			
			mysql_query("delete from adtrackz_split_test_groups where ID='$st_id'");
			mysql_query("delete from adtrackz_split_test_campaigns where split_test_ID='$st_id'");

			alert("The split test $st_label[0] has been deleted");
			echo "<br><br>";
			require "footer.php";
			exit;
		}
	}
	//SAVE CAMPAIGNS if from modify
	elseif($subaction == "save")
	{
		//error checking
		if (mysql_fetch_row(mysql_query("select name from adtrackz_split_test_groups where name='$st_name' && id !='$st_id'")))
			error("$st_name is already taken. ");
		elseif (strlen($st_name) > 20)
			error("The name you've chosen is too long. There is a 20 character maximum. ");
		elseif (!$st_name)
			error("You did not enter a name for the new split test. ");
		elseif($length < 2)
			error("You must add at least 2 campaigns to a split test");
		else
		{
			if ($tag)
				$tag = "checked";

			$old_stname = mysql_fetch_row(mysql_query("select name from adtrackz_split_test_groups where ID='$st_id'"));
			if ($old_stname[0] != $st_name)
				$msg = "<p><b>Warning:</b>You have changed the name for this split test. Consequently, any redirect files you have created for this split test will no longer work. To fix this problem, you can delete all redirect files associated with this split test and recreate them again, the new split test name will be used. Or you can simply change the split test name back to what it was previously.";

			mysql_query("update adtrackz_split_test_groups set name='$st_name', tagvisitor='$tag' where id='$st_id'");

			//delete them all
			mysql_query("delete from adtrackz_split_test_campaigns where split_test_id='$st_id'");

			//add from last page
			for($i=0;$i<$length;$i++)
			{
				$option = "option".$i;
				$campaign = $$option; //mysql query doesn't like $$option		
				$campaignID = mysql_fetch_row(mysql_query("select ID from adtrackz_campaigns where adcode='$campaign'"));
				mysql_query("insert into adtrackz_split_test_campaigns values('','$campaignID[0]','$st_id')");
			}
			alert("$st_name has been updated successfully.$msg");
		}
	}


	$stdata1 = mysql_query("select * from adtrackz_split_test_groups order by name asc");
	if (!mysql_num_rows($stdata1))
		echo "There are no split tests to display. <a href=\"st_new.php\">Click Here</a> to add a split test.";
	else
	{
		?>
		<br>
		<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
		<tr>
			<td class="tbhead1">Split Test</td>
			<td class="tbhead1">Campaign</td>
			<td class="tbhead1">Visitor Tag</td>
			<td class="tbhead1">Date Created</td>
			<td class="tbhead1">Url To Use</td>		
			<td class="tbhead1">Action</td>
		</tr>
		<?	
		while ($st = mysql_fetch_row($stdata1))
		{
			$campaigns = mysql_query("select campaignID from adtrackz_split_test_campaigns where split_test_ID='$st[0]'");
			?>
			<tr>
				<td class="tb2"><?=$st[1]?></td>
				<td class="tb2">
			<?
			for($i=0;$i<mysql_num_rows($campaigns);$i++)
			{
				$cID = mysql_fetch_row($campaigns);
				$adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID[0]'"));
				if ($i == mysql_num_rows($campaigns) - 1)
					echo $adcode[0];
				else
					echo "$adcode[0], ";
			}		
			?>
				</td>
				<td class="tb2">
					<?
					if (!$st[2])
						echo "&nbsp;";
					else
						echo $st[2];
					?>
				</td>
				<td class="tb2"><?=$st[3]?></td>
				<td>
					<textarea cols=20 rows=4>http://<?=$domain?>/<?=$udirectory?>/st.php?s=<?=$st[1]?></textarea>
				</td>
				<td class="tb2">
					
					<table width=100%>
					<tr>
						<td><span class="small"><a href="st_edit.php?action=modify&st_id=<?=$st[0]?>">modify</a></span></td>
					</tr>
					<tr>						
						<td><span class="small"><a href="st_edit.php?subaction=delete&st_id=<?=$st[0]?>">delete</a></span></td>
						
					</tr>

					<tr>						
						<td><span class="small"><a href="http://<?=$domain?>/<?=$udirectory?>/st.php?s=<?=$st[1]?>" target=_new>go to url</a></span></td>
					</tr>	
					</table>
				</td>
			</tr>
			<?
		}
		echo "</table>";
	}
}
//----------------------------\\
// MODIFY SPLIT TEST GROUPS
//----------------------------\\
elseif ($action == "modify")
{
	list($st_name, $tag) = mysql_fetch_row(mysql_query("select name,tagvisitor from adtrackz_split_test_groups where ID='$st_id'"));	
	?>
	<p class="small" align="left">
	Split Test > Edit
	</p>
	<p><u>Modifing Split Test Group</u></p>
	<p>You can modify the split test group  <?=$st_name?> here. Click save changes when you're done.</p>

	<SCRIPT language=javascript src="jsfunctions.js"></SCRIPT>
	<form method="post" name="modifyform" action="st_edit.php">
	<table width="90%" align="center">
	<tr>
		<td>Name</td>
		<td><input type=text name=st_name value=<?=$st_name?>></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>Visitor Tag</td>
		<td><input type=checkbox name=tag <?=$tag?>></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan=3>&nbsp;</td>
	</tr>
	<tr>
		<td>all campaigns</td>
		<td>&nbsp;</td>
		<td>campaigns in <?=$st_name?></td>
	</tr>
	<tr>
		<td>
			<select multiple name="allcampaigns" size=10>
			<?		
			$campaigndata = mysql_query("select * from adtrackz_campaigns order by adcode asc");
			while($campaign1 = mysql_fetch_row($campaigndata))
				echo "<option value=\"$campaign1[2]\">$campaign1[2]</option>";
			?>
			</select>
		</td>
		<td>
			<input type="button" name="add" value="Add To Split Test ->" onclick="AddCampaign();"  >
			<br><br>
			<input type="button" name="remove" value="Remove From Split Test" onclick="RemoveCampaign();"  >
		<td>
			<select multiple name="campaigns" size=10>
			<?		
			$campaignIDs = mysql_query("select campaignID from adtrackz_split_test_campaigns where split_test_id='$st_id'");
			while($cID = mysql_fetch_row($campaignIDs))
			{			
				$campaigndata = mysql_fetch_row(mysql_query("select ID,adcode from adtrackz_campaigns where ID='$cID[0]' "));
				echo "<option value=\"$campaigndata[1]\">$campaigndata[1]</option>";
			}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td colspan="4" align="center"><br>
			<input type="hidden" name="st_id" value="<?=$st_id?>">	
			<input type="hidden" name="subaction" value="save">
			<input type="button" value="Save Changes" onclick="AddNew();"  >
		<td>
	</tr>
	</table>
	</form>
	<?
}

require "footer.php";
?>